package heven.holt.wan.android.adapter

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import heven.holt.wan.android.fragment.*

const val HOME_ARTICLE_INDEX = 0
const val HOME_NAVIGATION_INDEX = 1
const val HOME_QA_INDEX = 2
const val HOME_PROJECT_INDEX = 3
const val HOME_MINE_INDEX = 4

class HomeViewPagerAdapter(
    fragment: Fragment
) : FragmentStateAdapter(fragment) {
    private val tabFragmentsCreators: Map<Int, () -> Fragment> = mapOf(
        HOME_ARTICLE_INDEX to { HomeFragment() },
        HOME_NAVIGATION_INDEX to { NavigationFragment() },
        HOME_QA_INDEX to { QAFragment() },
        HOME_PROJECT_INDEX to { ProjectFragment() },
        HOME_MINE_INDEX to { UserFragment() }
    )

    override fun getItemCount(): Int = tabFragmentsCreators.size

    override fun createFragment(position: Int): Fragment =
        tabFragmentsCreators[position]?.invoke() ?: throw IndexOutOfBoundsException()
}